<template>
  <div class="app-container">
    <el-form :model="queryParams"
             ref="queryForm"
             :inline="true"
             v-show="showSearch"
             label-width="68px">
      <el-form-item label="dtu地址	"
                    prop="addr">
        <el-input v-model="queryParams.addr"
                  placeholder="请输入dtu地址	"
                  clearable
                  size="small"
                  @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="dtu名称		"
                    prop="name">
        <el-input v-model="queryParams.name"
                  placeholder="请输入dtu名称	"
                  clearable
                  size="small"
                  @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="设备ID		"
                    prop="nodeId">
        <el-input v-model="queryParams.name"
                  placeholder="请输入设备ID	"
                  clearable
                  size="small"
                  @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="设备状态"
                    prop="status">
        <el-select v-model="queryParams.type"
                   placeholder="设备类型"
                   size="small"
                   clearable
                   @keyup.enter.native="handleQuery">

          <el-option
            v-for="type in deviceTypeOption"
            :key="'deviceTypeOption'+type.dictValue"

            :label="type.dictLabel"
            :value="type.dictValue"></el-option>

        </el-select>
      </el-form-item>
      <el-form-item label="设备状态"
                    prop="status">
        <el-select v-model="queryParams.status"
                   placeholder="设备状态"
                   size="small"
                   clearable
                   @keyup.enter.native="handleQuery">

          <el-option label="启用"
                     value="1"></el-option>
          <el-option label="禁用"
                     value="0"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary"
                   icon="el-icon-search"
                   size="mini"
                   @click="handleQuery">搜索
        </el-button>
        <el-button icon="el-icon-refresh"
                   size="mini"
                   @click="resetQuery">重置
        </el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10"
            class="mb8">
      <el-col :span="1.5">
        <el-button type="primary"
                   plain
                   icon="el-icon-plus"
                   size="mini"
                   @click="handleAdd"
                   v-hasPermi="['system:appsecret:add']">新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success"
                   plain
                   icon="el-icon-edit"
                   size="mini"
                   :disabled="single"
                   @click="handleUpdate"
                   v-hasPermi="['system:appsecret:edit']">修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger"
                   plain
                   icon="el-icon-delete"
                   size="mini"
                   :disabled="multiple"
                   @click="handleDelete"
                   v-hasPermi="['system:appsecret:remove']">删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning"
                   plain
                   icon="el-icon-download"
                   size="mini"
                   @click="handleExport"
                   v-hasPermi="['system:appsecret:export']">导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch"
                     @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading"
              :data="appsecretList"
              @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection"
                       width="55"
                       align="center"/>
      <el-table-column label="dtu地址	"
                       align="center"
                       prop="addr"
                       v-if="false"/>
      <el-table-column label="dtu名称	"
                       align="center"
                       prop="name"/>
      <el-table-column label="设备类型"
                       align="center"
                       prop="type">
        <template slot-scope="scope">
          <span v-if="scope.row.type == 1" style="color: #fadb14;">电</span>
          <span v-if="scope.row.type == 2" style="color: #40a9ff;">水</span>
        </template>
      </el-table-column>
      <el-table-column label="设备ID"
                       align="center"
                       prop="nodeId"/>
      <el-table-column label="品牌"
                       align="center"
                       prop="deviceBrand.brandName"/>
      <el-table-column label="设备状态"
                       align="center"
                       prop="status">

        <template slot-scope="scope">
          <span v-if="scope.row.status == 0">关闭</span>
          <span v-if="scope.row.status == 1">开启</span>

        </template>
      </el-table-column>
      <!-- <el-table-column label="用户地址" align="center" prop="addr"/> -->
      <el-table-column label="操作"
                       align="center"
                       class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini"
                     type="text"
                     icon="el-icon-edit"
                     @click="handleUpdate(scope.row)"
                     v-hasPermi="['system:appsecret:edit']">修改
          </el-button>
          <el-button size="mini"
                     type="text"
                     icon="el-icon-delete"
                     @click="handleDelete(scope.row)"
                     v-hasPermi="['system:appsecret:remove']">删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0"
                :total="total"
                :page.sync="queryParams.pageNum"
                :limit.sync="queryParams.pageSize"
                @pagination="getList"/>

    <!-- 添加或修改【请填写功能名称】对话框 -->
    <el-dialog :title="title"
               :visible.sync="open"
               width="500px"
               append-to-body>
      <el-form ref="form"
               :model="form"
               :rules="rules"
               label-width="100px">
        <el-form-item label="dtu地址		"
                      prop="addr">
          <el-input v-model="form.addr"
                    placeholder="请输入dtu地址	"/>
        </el-form-item>


        <el-form-item label="dtu名称		"
                      prop="name">
          <el-input v-model="form.name"
                    placeholder="请输入dtu名称	"/>
        </el-form-item>
        <el-form-item label="品牌"
                      pro p="brandId">
          <el-select v-model="form.brandId"
                     :disabled="true"
                     v-show="form.brandId">
            <el-option
              v-for="(item,index) in brandOption"
              :key="item.label+''+index"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
          <el-button type="text" @click="getBrand" style="margin-left: 20px;">选择品牌</el-button>
        </el-form-item>
        <el-form-item label="nodeId	"
                      prop="nodeId">
          <el-input v-model="form.nodeId"
                    placeholder="请输入nodeId"/>
        </el-form-item>
        <el-form-item label="设备状态	">
          <el-radio-group v-model="form.status">

            <el-radio v-model="form.status"
                      :label="1">启用
            </el-radio>
            <el-radio v-model="form.status"
                      :label="0">禁用
            </el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="设备类型	">
          <el-radio-group v-model="form.type">
            <el-radio v-model="form.type"
                      :label="1">电表
            </el-radio>
            <el-radio v-model="form.type"
                      :label="2">水表
            </el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer"
           class="dialog-footer">
        <el-button type="primary"
                   @click="submitForm">确 定
        </el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!--    品牌列表弹窗-->
    <el-dialog
      title="品牌列表"
      :visible.sync="brandOpen"
      width="30%"
    >

      <el-table
        v-if="brandList.rows"
        :data="brandList.rows"
        highlight-current-row
        @current-change="brandCurrent"
        style="width: 100%">
        <el-table-column
          prop="brandName"
          label="品牌名称	"
          width="180">
        </el-table-column>
        <el-table-column
          prop="content"
          label="品牌内容	"
          width="180">
        </el-table-column>
        <el-table-column
          label="品牌图片">
          <template slot-scope="scope">
            <el-image
              style="width: 100px; height: 100px"
              :preview-src-list="[scope.row.pic]"
              :src="scope.row.pic"></el-image>
          </template>
        </el-table-column>
        <el-table-column label="品牌类型" align="center" prop="type">
          <template slot-scope="scope">
            <span v-if="scope.row.type == 1">门锁</span>
            <span v-if="scope.row.type == 2">电表</span>
            <span v-if="scope.row.type == 3">水表</span>
            <span v-if="scope.row.type == 4">Dtu</span>
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-if="brandList.total>0"
        :total="brandList.total"
        :page.sync="brandQuery.pageNum"
        :limit.sync="brandQuery.pageSize"
        @pagination="getBrand"
      />
      <span slot="footer" class="dialog-footer">
        <el-button @click="brandOpen = false">取 消</el-button>
        <el-button type="primary" @click="brandOpen = false">确 定</el-button>
      </span>
    </el-dialog>

  </div>
</template>

<script>
import {
  listAppsecret,
  getAppsecret,
  delAppsecret,
  addAppsecret,
  updateAppsecret,
  exportAppsecret,
  changeUserStatus,
  getDevies
} from '@/api/lock/sysAppsecret'
import { getDevices } from '@/api/brand/brand'
import {
  getDevice,
  copyTable,
  getDeviceList,
  delDevice,
  updateDevice,
  addDevice,
  exportDevice
} from '@/api/device/device'
import { listBrand } from '@/api/brand/brand'
import { listDtu, getDtu, addDtu, updateDtu, delDtu, exportDtu } from '@/api/dtu/dtu'
import { listData } from '@/api/system/dict/data'

export default {
  name: 'Appsecret',
  components: {},
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 【请填写功能名称】表格数据
      appsecretList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      openView: false,
      // radio
      radio: undefined,
      // passwordType
      passwordType: 'password',
      // 状态数据字典
      statusOptions: [],
      deviceId: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: undefined,
        appid: undefined,
        secret: undefined,
        totalCount: undefined,
        transferCount: undefined,
        ips: undefined,
        phone: undefined,
        addr: undefined,
        status: undefined
      },
      brakeStatusOption: [
        {
          value: 1,
          label: '开'

        },
        {
          value: 0,
          label: '关'

        }
      ],
      deviceTypeOption: [],//设备列表
      brandList: [],//品牌列表
      brandQuery: {
        pageNum: 1,
        pageSize: 10
      },
      brandOpen: false,//选择品牌弹窗
      //品牌下拉列表数据
      brandOption:[],
      DtuList: [],//Dtu列表
      DtuQuery: { //Dtu请求参数
        pageNum: 1,
        pageSize: 10
      },
      DtuOpen: false,//选择Dtu弹窗
      DtuOption: {//Dtu下拉列表数据

      },
      // 表单参数
      form: {},

      // 表单校验
      rules: {
        phone: [
          { required: true, message: '手机号码不能为空', trigger: 'blur' },
          {
            pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/,
            message: '请输入正确的手机号码',
            trigger: 'blur'
          }
        ],
        name: [
          { required: true, message: '请输入用户名称', trigger: 'blur' }
        ],
        deviceId: [
          { required: true, message: '请输入设备ID', trigger: 'blur' },
          {
            pattern: /^\d{6,10}$/,
            message: '请输入6-10数字设备ID',
            trigger: 'blur'
          }
        ],
        status: [
          { required: true, message: '请选择状态', trigger: 'blur' }
        ],
        addr: [
          { required: true, message: '请输入地址', trigger: 'blur' }
        ],
        ips: [
          { required: true, message: '请输入', trigger: 'blur' }
        ],
        totalCount: [
          { required: true, message: '请输入', trigger: 'blur' },
          {
            pattern: /^[0-9]+$/,
            message: '请输入数字',
            trigger: 'blur'
          }
        ]
      },
      icon2: 'el-icon-warning-outline',
      icon1: 'el-icon-view'
    }
  },
  created() {
    this.getList()
    this.getDevices()
    this.deviceType()
  },
  methods: {
    /** 查询【请填写功能名称】列表 */
    getList() {
      this.loading = true
      listDtu(this.queryParams).then(res => {
        this.appsecretList = res.rows
        this.total = res.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        name: undefined,
        appid: undefined,
        secret: undefined,
        totalCount: undefined,
        transferCount: undefined,
        ips: undefined,
        phone: undefined,
        addr: undefined,
        createTime: undefined,
        createBy: undefined,
        updateTime: undefined,
        updateBy: undefined,
        status: '0'
      }
      this.resetForm('form')
    },
    //获取设备列表
    getDevices() {
      getDevices().then(res => {
        if (res.code == 200) {
          console.log(res)
        }
      })
    },
    deviceType() {
      listData({
        dictType: 'sys_device_type'
      }).then(res => {
        if (res.code == 200) {
          this.deviceTypeOption = res.rows
        }
      })
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加DTU'

    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      getDtu(id).then(response => {
        this.form = response.data
        this.open = true
        this.title = '修改DTU'
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          delete this.form.createTime;
          delete this.form.updateTime;
          delete this.form.agree;
          if (this.form.id != null) {
            updateDtu(this.form).then(response => {
              this.msgSuccess('修改成功')
              this.open = false
              this.getList()
            })
          } else {
            addDtu(this.form).then(response => {
              this.msgSuccess('新增成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$confirm('是否确认删除 编号为"' + ids + '"的数据项?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        return delDtu(ids)
      }).then(() => {
        this.getList()
        this.msgSuccess('删除成功')
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams
      this.$confirm('是否确认导出所有 数据项?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        return exportDevice(queryParams)
      }).then(response => {
        this.download(response.msg)
      })
    },
    //查询品牌
    getBrand() {
      this.brandOpen = true
      listBrand(this.brandQuery).then(res => {
        if (res.code == 200) {
          this.brandList = res
        }
      })
    },
    brandClose() { //关闭选择弹窗
      this.brandOption = []
    },
    brandCurrent(row) {//品牌选择
      if(row){
        this.form.brandId = row.id
        this.brandOption.push({
          label : row.brandName,
          value : row.id
        })
        this.brandOpen = false
      }
    },
    // 用户状态修改
    handleStatusChange(row) {
      changeUserStatus(row.id, row.status).then(response => {
        this.$message({
          message: '操作成功',
          type: 'warning'
        })
      })
    },

    //查看按钮
    handleWatch(row) {
      getDevice(row.id).then(response => {
        this.form = response.data
        this.openView = true
        this.title = '电列数据'
      })
    },
    recording(row) {
      copyTable(row.id).then(res => {
        console.log(res)
      })
    }

  }
}
</script>
